package com.zjitc.common.redis;

import com.zjitc.common.Gsonal;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/4/10 15:53
 * @description:
 */
public class MyRedisPool implements RedisCache{

  private final JedisPool pool;

  @Autowired
  public MyRedisPool(JedisPool pool) {this.pool = pool;}

  //=============================key============================

  @Override
  public void expire(String key,int time){
    try (Jedis jedis = pool.getResource()) {
      if (time > 0) {
        jedis.expire(key, time);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  @Override
  public long getExpire(String key){
    try (Jedis jedis = pool.getResource()) {
      return jedis.ttl(key);
    }
  }

  @Override
  public long moveExpire(String key){
    try (Jedis jedis = pool.getResource()) {
      return jedis.persist(key);
    }
  }

  @Override
  public boolean exists(String key){
    try (Jedis jedis = pool.getResource()) {
      return jedis.exists(key);
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }

  @Override
  public long del(String ... keys){
    long count = 0;
    try (Jedis jedis = pool.getResource()) {
      int i = 0;
      while (i < keys.length) {
        Long del = jedis.del(keys[i]);
        count += del;
        i++;
      }
      return count;
    }
  }

  //============================String=============================
  @Override
  public Object get(String key){
    try (Jedis jedis = pool.getResource()) {
      return key == null ? null : jedis.get(key);
    }
  }

  @Override
  public boolean set(String key,String value) {
    try (Jedis jedis = pool.getResource()) {
      jedis.set(key, value);
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }

  }

  @Override
  public boolean set(String key,String value, int time){
    try (Jedis jedis = pool.getResource()) {
      if (time > 0) {
        jedis.set(key, value);
        expire(key, time);
      }
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }

  @Override
  public long increase(String key, long delta){
    if(delta<0){
      throw new RuntimeException("递增因子必须大于0");
    }
    try (Jedis jedis = pool.getResource()) {
      return jedis.incrBy(key, delta);
    }
  }

  @Override
  public long decrease(String key, long delta){
    if(delta<0){
      throw new RuntimeException("递减因子必须大于0");
    }
    try (Jedis jedis = pool.getResource()) {
      return jedis.decrBy(key, delta);
    }
  }

  //================================Hash=================================

  @Override
  public boolean existsHash(String key,String field){
    try (Jedis jedis = pool.getResource()) {
      return jedis.hexists(key, field);
    }
  }

  @Override
  public String hashMapget(String key, String field){
    try (Jedis jedis = pool.getResource()) {
      return jedis.hget(key, field);
    }
  }

  @Override
  public Map<String, String> hashMapGetAll(String key){
    try (Jedis jedis = pool.getResource()) {
      return jedis.hgetAll(key);
    }
  }


  @Override
  public <T> List<T> hashListGetAll(String keys,Class<T> classOfT) {
    try (Jedis ignored = pool.getResource()) {
      Map<String, String> mapGetAll = hashMapGetAll(keys);
      List<T> list = new ArrayList<>();
      mapGetAll.forEach((key, value) -> {
        T t = Gsonal.fromJson(value, classOfT);
        list.add(t);
      });
      return list;
    }
  }

  @Override
  public <T> T hashgetObject(String key, String filed, Class<T> classOfT) {
    Jedis jedis = pool.getResource();
    try{
      String json = hashMapget(key, filed);
      T t = Gsonal.fromJson(json, classOfT);
      return t;
    }finally {
      jedis.close();
    }
  }

  @Override
  public Long hashset(String key,String filed,String value){
    try (Jedis jedis = pool.getResource()) {
      return jedis.hset(key, filed, value);
    }
  }

  @Override
  public String hashMapset(String key, Map<String,String > map){
    try (Jedis jedis = pool.getResource()) {
      return jedis.hmset(key, map);
    } catch (Exception e) {
      e.printStackTrace();
      return "存储中出错";
    }
  }

  @Override
  public String hashMapset(String key, Map<String,String> map, int time){
    try (Jedis jedis = pool.getResource()) {
      String hmset = jedis.hmset(key, map);
      expire(key, time);
      return hmset;
    } catch (Exception e) {
      e.printStackTrace();
      return "存储中出错";
    }
  }

  @Override
  public Long hashExistset(String key,String field,String value) {
    try (Jedis jedis = pool.getResource()) {
      return jedis.hsetnx(key, field, value);
    }
  }

  @Override
  public Long hashExistset(String key,String field,String  value,int time) {
    try (Jedis jedis = pool.getResource()) {
      Long aLong = jedis.hsetnx(key, field, value);
      if (time > 0) {
        expire(key, time);
      }
      return aLong;
    }
  }

  @Override
  public void delHash(String key, String... field){
    try (Jedis jedis = pool.getResource()) {
      jedis.hdel(key, field);
    }
  }

  //============================set=============================
  @Override
  public Set<String> getAllSet(String key){
    try (Jedis jedis = pool.getResource()) {
      return jedis.smembers(key);
    }
  }

  @Override
  public boolean existsSet(String key,String value){
    try (Jedis jedis = pool.getResource()) {
      return jedis.sismember(key, value);
    }
  }

  @Override
  public long addSet(String key, String...values) {
    try (Jedis jedis = pool.getResource()) {
      return jedis.sadd(key, values);
    } catch (Exception e) {
      e.printStackTrace();
      return 0;
    }
  }

  @Override
  public long addSet(String key,int time,String...values) {
    try (Jedis jedis = pool.getResource()) {
      Long count = jedis.sadd(key, values);
      if (time > 0) {
        expire(key, time);
      }
      return count;
    } catch (Exception e) {
      e.printStackTrace();
      return 0;
    }
  }

  @Override
  public long getSetLength(String key){
    try (Jedis jedis = pool.getResource()) {
      return jedis.scard(key);
    }
  }

  @Override
  public long removeSetby(String key, String ...values) {
    try (Jedis jedis = pool.getResource()) {
      return jedis.srem(key, values);
    }
  }

  //===============================list==================================
  @Override
  public long getListSize(String key){
    try (Jedis jedis = pool.getResource()) {
      return jedis.llen(key);
    }
  }

  @Override
  public Object getListIndex(String key,long index){
    try (Jedis jedis = pool.getResource()) {
      return jedis.lindex(key, index);
    }
  }

  @Override
  public void addListL(String key, String... value) {
    try (Jedis jedis = pool.getResource()) {
      jedis.lpush(key, value);
    }
  }

  @Override
  public boolean addListL(String key, String value, int time) {
    try (Jedis ignored = pool.getResource()) {
      addListL(key, value);
      if (time > 0) { expire(key, time);}
      return true;
    }
  }

  @Override
  public void addListR(String key, String... value) {
    try (Jedis jedis = pool.getResource()) {
      jedis.rpush(key, value);
    }
  }

  @Override
  public boolean addListR(String key, int time, String... value) {
    try (Jedis ignored = pool.getResource()) {
      addListR(key, value);
      if (time > 0) { expire(key, time);}
      return true;
    }
  }

  @Override
  public boolean setByIndex(String key, int index,String value) {
    try (Jedis jedis = pool.getResource()) {
      jedis.lset(key, index, value);
      return true;
    }
  }

  @Override
  public long lRemove(String key,long count,String value) {
    try (Jedis jedis = pool.getResource()) {
      return jedis.lrem(key, count, value);
    }
  }
}
